package com.kushe.site.controller;

import com.kushe.dao.*;
import com.kushe.entity.*;
import com.kushe.site.form.TopicsForm;
import com.kushe.util.KSController;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.nutz.dao.QueryResult;
import org.nutz.http.Http;
import org.nutz.json.Json;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Time;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * Created by WSDevotion on 2015/5/3.
 */
@Controller
@RequestMapping("topic")
public class ToipcsController extends KSController {

    @RequestMapping("/addTopics")
    public Object addTopics(@RequestParam(value = "file", required = false) MultipartFile file, @RequestParam(value = "page" ,required = false)int page,HttpServletRequest request,Topics topics){
        TopicsForm form = new TopicsForm();
        if(!form.validate(request)){
            System.out.println("表单");
            return "error";
        }
        //上传图片
        String filename = uploadImg(file, request);

        //上传信息
        topics.setImgurl("\\upload\\" + filename);
        Date date = new Date();
        Group group = GroupDao.get(topics.getGroup_id());
        //从session中得到user
        User user = (User) request.getSession().getAttribute("user");
        topics.setGroup_name(group.getName());
        topics.setAuthor_id(user.getId());
        topics.setUpdate_time(date);
        TopicsDao.addTopics(topics);
        //查询用户的小组
        return this.render("redirect:/topic/toTopicindex");

    }

    //进入小组话题页面
    @RequestMapping("toTopicindex")
    public Object toTopicsindex(@RequestParam(value = "page", defaultValue = "1", required = false) int page, HttpServletRequest request){
        //查询用户的小组
        //从session中得到user
        User user = (User) request.getSession().getAttribute("user");
        List<UserAndGroup> userAndGroupList = UserAndGroupDao.getUserAndGroupByuser_id(user.getId());
        int [] groups = new int [userAndGroupList.size()];
        List<Group> list = new LinkedList<Group>();
        for(int i=0; i<userAndGroupList.size(); i++){
            groups[i] = userAndGroupList.get(i).getGroup_id();
            Group g = GroupDao.get(groups[i]);
            list.add(g);
        }
        //int [] groups = {topics.getGroup_id()};
        //查找话题
        this.setAttr("topics", TopicsDao.getByTime(page, groups));
        QueryResult list1 = TopicsDao.getByTime(page, groups);
        //按num查找小组
        this.setAttr("groups", list);
        return this.render("group/group.html");
    }




    //话题的点赞
    @RequestMapping("toZanTopic")
    public Object toZanTopic(int id, HttpServletRequest request){
        Topics topics = TopicsDao.get(id);
        //从session中得到user
        User user = (User) request.getSession().getAttribute("user");
        //判断用户是否已经点赞
        Sign sign = SignDao.getSigns(user.getId(), id);
        if(sign!=null){
            this.render("redirect:/topic/goTopic?id=" + id);
            //return "redirect:/topic/goTopic?id=" + id;
        }
        sign = new Sign();
        sign.setUser_id(user.getId());
        sign.setTopic_id(id);
        SignDao.addSign(sign);
        topics.setZan(topics.getZan()+1);
        TopicsDao.updateTopics(topics);
        return this.render("redirect:/topic/goTopic?id=" + id);
        //return "redirect:/topic/goTopic?id=" + id;
    }

    @RequestMapping("deleteTopic")
    public Object deleteTopic(int id){
        //通过id查找话题
        Topics topics = TopicsDao.get(id);
        if(topics==null){
            return "error";
        }
        CommentDao.deleteTopics(id);
        TopicsDao.deleteTopics(id);
        return this.render("redirect:/group/toGroupindex?id=" + topics.getGroup_id());
        //return "redirect:/group/toGroupindex?id=" + topics.getGroup_id();
    }



    //删除帖子的评论
    @RequestMapping("deleteComment")
    public Object deleteComment(int id, HttpServletRequest request){
        //判断当前用户是不是发帖人或评论者
        Comment comment = CommentDao.get(id);
        if(comment==null){
            return "error";
        }
        //从session中得到user
        User user = (User) request.getSession().getAttribute("user");
        int user_id = user.getId();
        Topics t = TopicsDao.get(comment.getTopic_id());
        if(comment.getUser_id()==user_id || t.getAuthor_id()==user_id){
            CommentDao.deleteid(comment.getId());
            return this.render("redirect:/topic/goTopic?id=" + t.getId());
            //return "redirect:/topic/goTopic?id=" + t.getId();
        }
        return this.render("redirect:/topic/goTopic?id=" + t.getId());
        //return "redirect:/topic/goTopic?id=" + t.getId();
    }

    @RequestMapping("goGroups")
    public Object goGroups(Model model, @RequestParam(value = "page", defaultValue = "1",required = false)int page){
        //查询用户的小组
        List<UserAndGroup> userAndGroupList = UserAndGroupDao.getUserAndGroupByuser_id(6);
        int [] groups = new int [userAndGroupList.size()];
        for(int i=0; i<userAndGroupList.size(); i++){
            groups[i] = userAndGroupList.get(i).getGroup_id();
        }
        //int [] groups = {topics.getGroup_id()};
        //查找话题
        this.setAttr("topics", TopicsDao.getByTime(page, groups));
        //model.addAttribute("topics", TopicsDao.getByTime(page, groups));

        //按num查找小组
        this.setAttr("groups", GroupDao.listByHot(1, 0));
        //model.addAttribute("groups", GroupDao.listByHot(1, 0));
        return this.render("group/group.html");
        //return "group/group.html";
    }

    //话题页面
    @RequestMapping("/goTopic")
    public Object goTopic(int id, @RequestParam(value = "page", defaultValue = "1") int page, Model model, HttpServletRequest request){
        Topics topics = TopicsDao.get(id);
        if(topics==null) {
            return "error";
        }

        //查找小组内容
        Group group = GroupDao.get(topics.getGroup_id());
        //查找最新话题
        int [] group_id = {topics.getGroup_id()};
        //当前用户是不是发布人
        //从session中得到user
        User user = (User) request.getSession().getAttribute("user");
        if(topics.getAuthor_id()==user.getId()){
            this.setAttr("iscreater", 1);
            //model.addAttribute("iscreater", 1);
        }else{
            this.setAttr("iscreater", 0);
            //model.addAttribute("iscreater", 0);
        }
        this.setAttr("comments", CommentDao.getByTopics(page, id));
        this.setAttr("topicnow", TopicsDao.getByTime(1, group_id));
        this.setAttr("group", group);
        this.setAttr("topics", topics);
        return this.render("group/post-content.html");

    }

    //--------------------------------------------------返回json

    //点赞
    @RequestMapping("tozan")
    @ResponseBody
    public Object tozan(int id, HttpServletRequest request){
        //用id查找话题
        //从session中得到user
        User user = (User) request.getSession().getAttribute("user");
        Sign s = SignDao.getSigns(user.getId(), id);
        JSONObject json = new JSONObject();
        if(s!=null){
            json.put("success", 2);
            Topics topic = TopicsDao.get(id);
            topic.setZan(topic.getZan()-1);
            TopicsDao.updateTopics(topic);
            json.put("zan", topic.getZan());
            SignDao.delectSigns(s.getId());
            return json;
        }
        Sign sign = new Sign();
        sign.setUser_id(user.getId());
        sign.setTopic_id(id);
        SignDao.addSign(sign);
        Topics topic = TopicsDao.get(id);
        topic.setZan(topic.getZan()+1);
        TopicsDao.updateTopics(topic);

        json.put("zan", topic.getZan());
        json.put("success", 1);
        return json;
    }

    //话题评论分页
    @RequestMapping("getCommentsPage")
    @ResponseBody
    public Object getCommentspage(int page, int topic_id, HttpServletRequest request){
        QueryResult comment = CommentDao.getByTopics(page, topic_id);
        List<Comment> list = (List<Comment>) comment.getList();
        JSONArray a = new JSONArray();
        JSONObject json1 = new JSONObject();

        if(comment.getPager().getPageCount()==page){
            json1.put("success","2");
        }else{
            json1.put("success","1");
        }
        for(Comment g : list){
            String j = Json.toJson(g);
            a.add(j);
        }
        //当前页数
        json1.put("page", comment.getPager().getPageNumber());
        //从session中得到user
        User user = (User) request.getSession().getAttribute("user");
        //当前用户是不是发布人
        Topics topics = TopicsDao.get(topic_id);
        if(topics.getAuthor_id()==user.getId()){
            json1.put("iscreater", 1);
        }else{
            json1.put("iscreater", 0);
        }
        json1.put("comments",a);

        return json1;

    }
}
